package siau.android.base;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.le.ScanFilter;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import hhm.android.library.LogUtils;
import hhm.android.library.bus.Bus;
import siau.android.http.model.ChildModel;
import siau.android.http.model.DeviceModel;
import siau.android.http.model.RecordHeightDataRequest;
import siau.android.http.model.RecordWeightDataRequest;
import siau.android.library.KeyString;

public class BlueToothUtil {
    private static BlueToothUtil blueToothUtil;
    private final ArrayList<ScanFilter> mScanFilters = new ArrayList<>();

    private boolean hasScanning = false;
    private Date scanningDate;

    public BluetoothAdapter mBluetoothAdapter;
    private CallBack callBack;
    private ControlCallBack controlCallBack;

    private final ArrayList<MyBlueToothBean> connectDevice = new ArrayList<>();
    public MutableLiveData<Integer> connectDeviceNum = new MutableLiveData<>(0);

    public ArrayList<MyBlueToothBean> scanDevices = new ArrayList<>();
    public MutableLiveData<Integer> scanDevicesNum = new MutableLiveData<>(0);

    private HashMap<String, String> oldDataMap = new HashMap<>();
    private BluetoothGatt bluetoothGatt;

    private MyBlueToothBean sendReturnWeightBean;
    private MyBlueToothBean sendReturnHeightBean;

    private BlueToothUtil() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        ScanFilter scanFilter = new ScanFilter.Builder().setDeviceName("SCALE").build();
        mScanFilters.add(scanFilter);
    }

    public static synchronized BlueToothUtil getInstance() {
        if (blueToothUtil == null) {
            synchronized (BlueToothUtil.class) {
                if (blueToothUtil == null) {
                    blueToothUtil = new BlueToothUtil();
                }
            }
        }
        return blueToothUtil;
    }

    public void pairBlueTooth() {
        if (hasScanning) {
            Date now = new Date();
            if (now.getTime() - scanningDate.getTime() >= 10000) {//已经扫了10s
                mBluetoothAdapter.stopLeScan(mScanCallBack);
                mBluetoothAdapter.startLeScan(mScanCallBack);
                hasScanning = true;
                scanningDate = now;
            }
        } else {
            mBluetoothAdapter.startLeScan(mScanCallBack);
            hasScanning = true;
            scanningDate = new Date();
        }
    }

    BluetoothAdapter.LeScanCallback mScanCallBack = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            try {
                byte[] bytes = scanRecord;

                //解析初始化数据包
                String ffString = "";
                int index = 0;
                while (bytes[index] != 0) {
                    if (bytes[index + 1] == -1) {//type 0xFF
                        byte[] ffBytes = new byte[bytes[index] - 1];
                        if (ffBytes.length >= 0)
                            System.arraycopy(bytes, index + 2, ffBytes, 0, ffBytes.length);
                        ffString = bytesToHexString(ffBytes);
                        if (ffString.startsWith("000")) {
                            LogUtils.Companion.d("hhm", "我是扫描结果   " + ffString);
                        }
                        break;
                    } else {
                        index += bytes[index] + 1;
                    }
                }

                //判断设备类型 判断是否是已绑定设备 已绑定直接绑定 未绑定添加到扫描列表中
                if (!TextUtils.isEmpty(ffString)) {
                    MyBlueToothBean mb = new MyBlueToothBean(device, ffString);
                    assert ffString != null;
                    if (ffString.startsWith("0004")) {//体重秤
                        boolean hasConnect = false;
                        for (DeviceModel dm : SBApplication.userData.getDeviceList()) {
                            if (ffString.equals(dm.getBluetoothCode())) {
                                if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                                    mb.setDeviceType(1);
                                    LogUtils.Companion.e("hhm", "我去连接了  " + mb.getBluetoothCode());
                                    connect(mb, false);
                                }
                                hasConnect = true;
                                break;
                            }
                        }
                        if (!hasConnect) {
                            boolean hasAdd = false;
                            for (MyBlueToothBean bean : scanDevices) {
                                if (bean.getBluetoothCode().equals(mb.getBluetoothCode())) {
                                    hasAdd = true;
                                    break;
                                }
                            }
                            if (!hasAdd) {
                                mb.setDeviceType(1);
                                scanDevices.add(mb);
                                scanDevicesNum.setValue(scanDevices.size());
                            }
                        }
                    } else if (ffString.startsWith("0005")) {//身高仪
                        boolean hasConnect = false;
                        for (DeviceModel dm : SBApplication.userData.getDeviceList()) {
                            if (ffString.equals(dm.getBluetoothCode())) {
                                if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                                    mb.setDeviceType(2);
                                    LogUtils.Companion.e("hhm", "我去连接了  " + mb.getBluetoothCode());
                                    connect(mb, false);
                                }
                                hasConnect = true;
                                break;
                            }
                        }
                        if (!hasConnect) {
                            boolean hasAdd = false;
                            for (MyBlueToothBean bean : scanDevices) {
                                if (bean.getBluetoothCode().equals(mb.getBluetoothCode())) {
                                    hasAdd = true;
                                    break;
                                }
                            }
                            if (!hasAdd) {
                                mb.setDeviceType(2);
                                scanDevices.add(mb);
                                scanDevicesNum.setValue(scanDevices.size());
                            }
                        }
                    } else if (ffString.startsWith("0006")) {//电动牙刷
                        boolean hasConnect = false;
                        for (DeviceModel dm : SBApplication.userData.getDeviceList()) {
                            if (ffString.equals(dm.getBluetoothCode())) {
                                if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                                    mb.setDeviceType(3);
                                    LogUtils.Companion.e("hhm", "我去连接了  " + mb.getBluetoothCode());
                                    connect(mb, false);
                                }
                                hasConnect = true;
                                break;
                            }
                        }
                        if (!hasConnect) {
                            boolean hasAdd = false;
                            for (MyBlueToothBean bean : scanDevices) {
                                if (bean.getBluetoothCode().equals(mb.getBluetoothCode())) {
                                    hasAdd = true;
                                    break;
                                }
                            }
                            if (!hasAdd) {
                                mb.setDeviceType(3);
                                scanDevices.add(mb);
                                scanDevicesNum.setValue(scanDevices.size());
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                LogUtils.Companion.e("hhm", "报错的蓝牙初始化数据包---" + scanRecord);
            }
        }
    };

    public void sendMsg(MyBlueToothBean bean, byte[] b) {
        bean.getWriteCharacteristic().setValue(b);
        bean.getGatt().writeCharacteristic(bean.getWriteCharacteristic());
    }

    //BLE蓝牙链接回调
    class MyBluetoothGattCallback extends BluetoothGattCallback {
        private final MyBlueToothBean mb;

        public MyBluetoothGattCallback(MyBlueToothBean mb) {
            this.mb = mb;
        }

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothGatt.STATE_CONNECTED) {
                oldDataMap.put(mb.getBluetoothCode(), "");
                mb.setGatt(gatt);
                connectDevice.add(mb);
                handler.sendEmptyMessage(55);
                gatt.discoverServices();
                if (callBack != null) {
                    callBack.connectSuccess(mb);
                }
                if (!TextUtils.isEmpty(KeyString.token)) {
                    pairBlueTooth();
                }
            }
            if (newState == BluetoothGatt.STATE_DISCONNECTED) {
                LogUtils.Companion.d("hhm", "   STATE_DISCONNECTED");
                gatt.disconnect();
                gatt.close();
                connectDevice.remove(mb);
                oldDataMap.put(mb.getBluetoothCode(), "");
                handler.sendEmptyMessage(55);
                if (!TextUtils.isEmpty(KeyString.token)) {
                    pairBlueTooth();
                }
                if (callBack != null) {
                    callBack.connectFail();
                }
            }
            if (newState == BluetoothGatt.STATE_DISCONNECTING) {
                LogUtils.Companion.d("hhm", "STATE_DISCONNECTING");
                if (callBack != null) {
                    callBack.connectFail();
                }
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            //获取服务列表
            List<BluetoothGattService> servicesList = gatt.getServices();

            for (int i = 0; i < servicesList.size(); i++) {
                if (i == servicesList.size() - 1) {
                    BluetoothGattService gattService = servicesList.get(i);
                    String serviceUUID = gattService.getUuid().toString();
                    mb.setServiceUuid(serviceUUID);

                    List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
                    for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                        String aCharUUID = gattCharacteristic.getUuid().toString();

                        List<BluetoothGattDescriptor> descriptors = gattCharacteristic.getDescriptors();
                        if (descriptors != null && descriptors.size() > 0) {
                            for (BluetoothGattDescriptor dgd : descriptors) {
                                String dgdUuid = dgd.getUuid().toString();

                                mb.setDesUuid(dgdUuid);
                                mb.setNotifyUuid(aCharUUID);
                            }
                        } else {
                            mb.setWriteUuid(aCharUUID);
                        }
                    }
                    mb.setBluetoothDevice(gatt.getDevice());
                    prepareUuidRegister(mb);
                }
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            StringBuilder var5 = new StringBuilder();
            int var3 = 0;

            for (int var4 = characteristic.getValue().length; var3 < var4; ++var3) {
                var5.append(Integer.toHexString(characteristic.getValue()[var3] & 255));
                var5.append(" ");
            }
            LogUtils.Companion.e("hhm", "onCharacteristicChanged：" + var5.toString());

            String stringData = var5.toString();

            Message obtain = Message.obtain();
            obtain.what = 5555;
            BluetoothDataModel bluetoothDataModel = new BluetoothDataModel();
            bluetoothDataModel.dataString = stringData;
            bluetoothDataModel.mb = mb;
            obtain.obj = bluetoothDataModel;
            handler.sendMessage(obtain);
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {//descriptor读
            LogUtils.Companion.e("hhm", "数据接收了哦" + bytesToHexString(descriptor.getValue()) + "        " + status);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            LogUtils.Companion.e("hhm", "数据接收了哦" + bytesToHexString(characteristic.getValue()));
            byte[] value = characteristic.getValue();
            String result;
            result = new String(value, StandardCharsets.UTF_8);
            LogUtils.Companion.e("hhm", "收到蓝牙数据：" + result + "        " + status);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                LogUtils.Companion.e("hhm", "onCharacteristicWrite" + "        " + status);
                LogUtils.Companion.e("hhm", bytesToHexString(characteristic.getValue()));
            }
            super.onCharacteristicWrite(gatt, characteristic, status);
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
            }
            LogUtils.Companion.e("hhm", "onDescriptorWrite" + "        " + status);
        }
    }

    public void sendResultToWeightMachine(MyBlueToothBean mb, double weightNum, double weight) {
        if (mb == null) {
            mb = sendReturnWeightBean;
        }
        if (mb == null) {
            return;
        }
        if (weight + 3 < weightNum) {//比上次小3kg
            byte[] result = new byte[]{(byte) 0xA7, 0x00, 0x04, 0x02, 0x03, 0x02, 0x0B, 0x7A};
            sendMsg(mb, result);
        } else if (weight - 5 > weightNum) {//比上次重5kg
            byte[] result = new byte[]{(byte) 0xA7, 0x00, 0x04, 0x02, 0x03, 0x03, 0x0C, 0x7A};
            sendMsg(mb, result);
        } else if (weight - 3 > weightNum) {//比上次重3kg
            byte[] result = new byte[]{(byte) 0xA7, 0x00, 0x04, 0x02, 0x03, 0x00, 0x09, 0x7A};
            sendMsg(mb, result);
        } else if (weight - 1 > weightNum) {//比上次重1kg
            byte[] result = new byte[]{(byte) 0xA7, 0x00, 0x04, 0x02, 0x03, 0x01, 0x0A, 0x7A};
            sendMsg(mb, result);
        }
    }

    public void sendResultToHeightMachine(MyBlueToothBean mb, double heightNum, double height) {
        if (mb == null) {
            mb = sendReturnHeightBean;
        }
        if (mb == null) {
            return;
        }
        if (height - 5 > heightNum) {//比上次长5cm
            byte[] result = new byte[]{(byte) 0xA7, 0x00, 0x05, 0x02, (byte) 0x86, 0x01, (byte) 0x8E, 0x7A};
            sendMsg(mb, result);
        } else if (height - 2 > heightNum) {//比上次长2cm
            byte[] result = new byte[]{(byte) 0xA7, 0x00, 0x05, 0x02, (byte) 0x86, 0x00, (byte) 0x8D, 0x7A};
            sendMsg(mb, result);
        }
    }

    public void connect(MyBlueToothBean mb, boolean storeLocal) {//是否缓存在本地用于取消连接
        BluetoothGatt bluetoothGatt = mb.getBluetoothDevice().connectGatt(SBApplication.applicationContext, false, new MyBluetoothGattCallback(mb));
        if (storeLocal) {
            this.bluetoothGatt = bluetoothGatt;
        }
    }

    public void cancelConnect() {
        if (bluetoothGatt != null) {
            bluetoothGatt.disconnect();
            bluetoothGatt.close();
        }
    }

    public void disConnect() {
        for (MyBlueToothBean bean : connectDevice) {
            bean.getGatt().disconnect();
            bean.getGatt().close();
        }
        connectDevice.clear();
        connectDeviceNum.setValue(connectDevice.size());
    }

    public void disConnect(MyBlueToothBean bean) {
        bean.getGatt().disconnect();
        bean.getGatt().close();
        connectDeviceNum.setValue(connectDevice.size());
    }

    public void stopScan() {
        mBluetoothAdapter.stopLeScan(mScanCallBack);
//        mBluetoothAdapter.getBluetoothLeScanner().stopScan(scanCallback);
    }

    public void prepareUuidRegister(MyBlueToothBean myBlueToothBean) {
        BluetoothGattService service = myBlueToothBean.getGatt().getService(UUID.fromString(myBlueToothBean.getServiceUuid()));
        if (service != null) {
            BluetoothGattCharacteristic notifyCharacteristic = service.getCharacteristic(UUID.fromString(myBlueToothBean.getNotifyUuid()));
            myBlueToothBean.getGatt().setCharacteristicNotification(notifyCharacteristic, true);

            BluetoothGattCharacteristic writeCharacteristic = service.getCharacteristic(UUID.fromString(myBlueToothBean.getWriteUuid()));
            myBlueToothBean.setWriteCharacteristic(writeCharacteristic);

            BluetoothGattDescriptor descriptor = notifyCharacteristic.getDescriptor(UUID.fromString(myBlueToothBean.getDesUuid()));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            notifyCharacteristic.addDescriptor(descriptor);

            myBlueToothBean.getGatt().writeDescriptor(descriptor);

            myBlueToothBean.getGatt().readCharacteristic(notifyCharacteristic);

            Message obtain = Message.obtain();
            obtain.obj = myBlueToothBean;
            handler.sendMessageDelayed(obtain, 500);

        }
        pairBlueTooth();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 555) {
                int childId = msg.arg1;
                MainPageChangeData mpcd = new MainPageChangeData();
                mpcd.deviceType = msg.arg2;
                mpcd.childId = childId;
                SBApplication.Companion.setCommunityChildId(childId);
                for (int i = 0; i < SBApplication.Companion.getUserData().getChildren().size(); i++) {
                    SBApplication.Companion.getUserData().getChildren().get(i).setSelect(SBApplication.Companion.getUserData().getChildren().get(i).getChildId() == childId);
                }
                SBApplication.Companion.getMainPageChange().setValue(mpcd);
            } else if (msg.what == 5555) {
                BluetoothDataModel bluetoothDataModel = (BluetoothDataModel) msg.obj;
                analyseData(bluetoothDataModel);
            } else if (msg.what == 55) {//刷新连接设备的数量
                connectDeviceNum.setValue(connectDevice.size());
            } else {
                MyBlueToothBean myBlueToothBean = (MyBlueToothBean) msg.obj;
                if (myBlueToothBean.getDeviceType() == 1) {//体重秤
                    //检查体重秤可调信息
                    if (!myBlueToothBean.getGetTrigger()) {
                        myBlueToothBean.setGetTrigger(true);
                        byte[] b = new byte[]{(byte) 0xA7, 0x00, 0x04, 0x01, (byte) 0x87, (byte) 0x8C, 0x7A};
                        sendMsg(myBlueToothBean, b);
                    }
                } else if (myBlueToothBean.getDeviceType() == 2) {//身高仪
                    //检查身高仪可调信息
                    if (!myBlueToothBean.getGetTrigger()) {
                        myBlueToothBean.setGetTrigger(true);
                        byte[] b = new byte[]{(byte) 0xA7, 0x00, 0x05, 0x01, (byte) 0x87, (byte) 0x8D, 0x7A};
                        sendMsg(myBlueToothBean, b);
                    }
                }
            }
        }
    };

    private void analyseData(BluetoothDataModel bluetoothDataModel) {
        String stringData = bluetoothDataModel.dataString;
        MyBlueToothBean mb = bluetoothDataModel.mb;
        String[] dataSplit = stringData.split(" ");
        if (mb.getDeviceType() == 1) {//体重秤
            if (stringData.startsWith("a7 0 4 5 1") && dataSplit.length == 11) {//体重锁定数据
                String s = oldDataMap.get(mb.getBluetoothCode());
                if (!stringData.equals(s)) {
                    String[] oldData = s.split(" ");
                    if (oldData.length >= 8 && !dataSplit[7].equals(oldData[7])) {
                        oldDataMap.put(mb.getBluetoothCode(), stringData);
                        return;
                    }
                    oldDataMap.put(mb.getBluetoothCode(), stringData);
                    if (dataSplit[7].equals("0")) {//体重单位kg
                        BigInteger bi = new BigInteger(dataSplit[5] + (dataSplit[6].length() == 1 ? "0" + dataSplit[6] : dataSplit[6]), 16);
                        int bei = (int) Math.pow(10, Double.parseDouble(dataSplit[8]));
                        double weight = Double.parseDouble(bi.toString()) / bei;
                        LogUtils.Companion.d("hhm", "体重：" + weight);

                        if (/*(SBApplication.Companion.getStartCalculate().getValue() && SBApplication.Companion.getCalculateType() == 1) ||*/ SBApplication.userData.getChildren().size() == 1) {
                            String weightString = "";
                            int httpChildId = -1;
//                            if (SBApplication.userData.getChildren().size() == 1) {
                            weightString = String.valueOf(SBApplication.userData.getChildren().get(0).getWeight());
//                                SBApplication.Companion.setCalculateChild(SBApplication.userData.getChildren().get(0).getChildId());
                            httpChildId = SBApplication.userData.getChildren().get(0).getChildId();
//                                SBApplication.Companion.setCalculateType(1);
//                            } else {
//                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
//                                        weightString = String.valueOf(cm.getWeight());
//                                    }
//                                }
//                                httpChildId = SBApplication.Companion.getCalculateChild();
//                            }

                            double weightNum = 0.0;
                            if (!TextUtils.isEmpty(weightString)) {
                                weightNum = Double.parseDouble(weightString);
                            }
                            if (SBApplication.Companion.getUserData().getWeightUnit().equals("KG")) {
                                sendResultToWeightMachine(mb, weightNum, weight);
                            } else if (SBApplication.Companion.getUserData().getWeightUnit().equals("JIN")) {
                                sendResultToWeightMachine(mb, weightNum / 2, weight);
                            } else {
                                sendResultToWeightMachine(mb, weightNum / 2.2, weight);
                            }

                            RecordWeightDataRequest r = new RecordWeightDataRequest(httpChildId, new Date().getTime(), 1, "" + weight, "KG");
                            new HttpHelper().recordWeight(r).subscribe(o -> {
//                                if (SBApplication.Companion.getStartCalculate().getValue()) {
//                                    SBApplication.Companion.getStartCalculate().setValue(false);
//                                }
                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
                                    cm.setWeight(weight);
//                                    }
                                }

                                Message obtain = Message.obtain();
                                obtain.what = 555;
                                obtain.arg1 = r.getChildId();
                                obtain.arg2 = 1;
                                handler.sendMessage(obtain);
                            }, err -> {
                                Toast.makeText(SBApplication.applicationContext, err.getMessage(), Toast.LENGTH_SHORT).show();
                            });
                        } else {
                            if (SBApplication.Companion.getAppForeground().getValue()) {
                                sendReturnWeightBean = mb;
                                Intent i = new Intent(SBApplication.applicationContext, GetBlueToothDataActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.putExtra(KeyString.data, weight);
                                i.putExtra(KeyString.type, 1);
                                i.putExtra(KeyString.cla, 0);
                                SBApplication.applicationContext.startActivity(i);
                            }
                        }
                    } else if (dataSplit[7].equals("6")) {
                        BigInteger bi = new BigInteger(dataSplit[5] + (dataSplit[6].length() == 1 ? "0" + dataSplit[6] : dataSplit[6]), 16);
                        int bei = (int) Math.pow(10, Double.parseDouble(dataSplit[8]));
                        double weight = Double.parseDouble(bi.toString()) / bei;
                        LogUtils.Companion.d("hhm", "体重：-----" + weight);

                        if (/*(SBApplication.Companion.getStartCalculate().getValue() && SBApplication.Companion.getCalculateType() == 1) ||*/ SBApplication.userData.getChildren().size() == 1) {
                            String weightString = "";
                            int httpChildId = -1;
//                            if (SBApplication.userData.getChildren().size() == 1) {
                            weightString = String.valueOf(SBApplication.userData.getChildren().get(0).getWeight());
//                                SBApplication.Companion.setCalculateChild(SBApplication.userData.getChildren().get(0).getChildId());
                            httpChildId = SBApplication.userData.getChildren().get(0).getChildId();
//                                SBApplication.Companion.setCalculateType(1);
//                            } else {
//                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
//                                        weightString = String.valueOf(cm.getWeight());
//                                    }
//                                }
//                                httpChildId = SBApplication.Companion.getCalculateChild();
//                            }

                            double weightNum = 0.0;
                            if (!TextUtils.isEmpty(weightString)) {
                                weightNum = Double.parseDouble(weightString);
                            }
                            if (SBApplication.Companion.getUserData().getWeightUnit().equals("KG")) {
                                sendResultToWeightMachine(mb, weightNum, weight);
                            } else if (SBApplication.Companion.getUserData().getWeightUnit().equals("JIN")) {
                                sendResultToWeightMachine(mb, weightNum / 2, weight);
                            } else {
                                sendResultToWeightMachine(mb, weightNum / 2.2, weight);
                            }

                            RecordWeightDataRequest r = new RecordWeightDataRequest(httpChildId, new Date().getTime(), 1, "" + weight, "KG");
                            new HttpHelper().recordWeight(r).subscribe(o -> {
//                                if (SBApplication.Companion.getStartCalculate().getValue()) {
//                                    SBApplication.Companion.getStartCalculate().setValue(false);
//                                }
                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
                                    double lbWeight = weight * 2.2046;
                                    String format = String.format("%.1f", lbWeight);
                                    cm.setWeight(Double.parseDouble(format));
//                                    }
                                }

                                Message obtain = Message.obtain();
                                obtain.what = 555;
                                obtain.arg2 = 1;
                                obtain.arg1 = r.getChildId();
                                handler.sendMessage(obtain);
                            }, err -> {
                                Toast.makeText(SBApplication.applicationContext, err.getMessage(), Toast.LENGTH_SHORT).show();
                            });
                        } else {
                            if (SBApplication.Companion.getAppForeground().getValue()) {
                                sendReturnWeightBean = mb;
                                Intent i = new Intent(SBApplication.applicationContext, GetBlueToothDataActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.putExtra(KeyString.data, weight);
                                i.putExtra(KeyString.type, 1);
                                i.putExtra(KeyString.cla, 4);
                                SBApplication.applicationContext.startActivity(i);
                            }
                        }
                    }
                }
            } else if (stringData.startsWith("a7 0 4 5 2")) {//体重非锁定数据
                oldDataMap.put(mb.getBluetoothCode(), stringData);
            } else if (stringData.startsWith("a7 0 4 2 86")) {//体重秤回复语音开关指令
                if (stringData.startsWith("a7 0 4 2 86 0")) {//成功
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.VoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceOpenSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.VoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceCloseSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                } else if (stringData.startsWith("a7 0 4 2 86 1")) {//失败
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.VoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceCloseSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.VoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceOpenSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                }
            } else if (stringData.startsWith("a7 0 4 2 8a")) {//体重秤回复简化语音开关指令
                if (stringData.startsWith("a7 0 4 2 8a 0")) {//成功
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.SimpleVoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceOpenSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.SimpleVoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceCloseSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                } else if (stringData.startsWith("a7 0 4 2 8a 1")) {//失败
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.SimpleVoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceCloseSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.SimpleVoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceOpenSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                }
            } else if (stringData.startsWith("a7 0 4 4 84")) {//体重秤回复鞋码开关指令
                String[] s = stringData.split(" ");
                if (s[6].equals("0")) {//开
                    if (controlCallBack != null) {
                        controlCallBack.shoeSizeOpenSuccess(mb.getBluetoothCode());
                    }
                } else if (s[6].equals("1")) {//关
                    if (controlCallBack != null) {
                        controlCallBack.shoeSizeCloseSuccess(mb.getBluetoothCode());
                    }
                }
                mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
            } else if (stringData.startsWith("a7 0 4 2 82")) {//体重秤回复单位控制指令
                if (stringData.startsWith("a7 0 4 2 82 0")) {//成功
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.classKGtoLB) {
                        if (controlCallBack != null) {
                            controlCallBack.classKGtoLBSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.classLBtoKG) {
                        if (controlCallBack != null) {
                            controlCallBack.classLBtoKGSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                } else if (stringData.startsWith("a7 0 4 2 82 1")) {//失败
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.classKGtoLB) {
                        if (controlCallBack != null) {
                            controlCallBack.classLBtoKGSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.classLBtoKG) {
                        if (controlCallBack != null) {
                            controlCallBack.classKGtoLBSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                }
            } else if (stringData.startsWith("a7 0 4 6 88")) {//体重秤设备可调试状态返回
                String[] s = stringData.split(" ");
                if (s[5].equals("0")) {//鞋码   0 开  1  关
                    mb.shoeSizeTrigger = true;
                    if (controlCallBack != null) {
                        controlCallBack.shoeSizeOpenSuccess(mb.getBluetoothCode());
                    }
                } else if (s[5].equals("1")) {
                    mb.shoeSizeTrigger = false;
                    if (controlCallBack != null) {
                        controlCallBack.shoeSizeCloseSuccess(mb.getBluetoothCode());
                    }
                }
                if (s[7].equals("0")) {//语音   0 开  1  关
                    mb.voiceTrigger = true;
                    if (controlCallBack != null) {
                        controlCallBack.voiceOpenSuccess(mb.getBluetoothCode());
                    }
                } else if (s[7].equals("1")) {
                    mb.voiceTrigger = false;
                    if (controlCallBack != null) {
                        controlCallBack.voiceCloseSuccess(mb.getBluetoothCode());
                    }
                }
                if (s[8].equals("0")) {//简化语音   0 开  1  关
                    mb.simpleVoiceTrigger = true;
                    if (controlCallBack != null) {
                        controlCallBack.simpleVoiceOpenSuccess(mb.getBluetoothCode());
                    }
                } else if (s[8].equals("1")) {
                    mb.simpleVoiceTrigger = false;
                    if (controlCallBack != null) {
                        controlCallBack.simpleVoiceCloseSuccess(mb.getBluetoothCode());
                    }
                }
                if (s[9].equals("0")) {//体重单位 0 kg 1 磅
                    mb.classTrigger = 0;
                    if (controlCallBack != null) {
                        controlCallBack.classLBtoKGSuccess(mb.getBluetoothCode());
                    }
                } else if (s[9].equals("6")) {
                    mb.classTrigger = 1;
                    if (controlCallBack != null) {
                        controlCallBack.classKGtoLBSuccess(mb.getBluetoothCode());
                    }
                }
            }
        } else if (mb.getDeviceType() == 2) {//身高仪
            if (stringData.startsWith("a7 0 5 6") && dataSplit.length == 12) {//身高数据
                if (!stringData.equals(oldDataMap.get(mb.getBluetoothCode()))) {
                    oldDataMap.put(mb.getBluetoothCode(), stringData);
                    if (dataSplit[8].equals("0")) {
                        BigInteger bigCm = new BigInteger(dataSplit[4], 16);
                        BigInteger smallCm = new BigInteger(dataSplit[5], 16);
                        double height;
                        if (smallCm.toString().length() == 1) {
                            height = Double.parseDouble((bigCm.toString()) + ".0" + smallCm.toString());
                        } else {
                            height = Double.parseDouble((bigCm.toString()) + "." + smallCm.toString());
                        }
                        LogUtils.Companion.d("hhm", "身高：" + height);

                        if (/*(SBApplication.Companion.getStartCalculate().getValue() && SBApplication.Companion.getCalculateType() == 2) ||*/ SBApplication.userData.getChildren().size() == 1) {
                            String heightString = "";
                            int httpChildId = -1;
//                            if (SBApplication.userData.getChildren().size() == 1) {
                            heightString = String.valueOf(SBApplication.userData.getChildren().get(0).getHeight());
//                                SBApplication.Companion.setCalculateChild(SBApplication.userData.getChildren().get(0).getChildId());
                            httpChildId = SBApplication.userData.getChildren().get(0).getChildId();
//                                SBApplication.Companion.setCalculateType(2);
//                            } else {
//                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
//                                        heightString = String.valueOf(cm.getHeight());
//                                    }
//                                }
//                                httpChildId = SBApplication.Companion.getCalculateChild();
//                            }

                            double heightNum = 0.0;
                            if (!TextUtils.isEmpty(heightString)) {
                                heightNum = Double.parseDouble(heightString);
                            }
                            if (SBApplication.Companion.getUserData().getHeightUnit().equals("CM")) {
                                sendResultToHeightMachine(mb, heightNum, height);
                            } else {
                                if (heightString.contains(".")) {
                                    String[] split = heightString.split("\\.");
                                    sendResultToHeightMachine(mb, Double.parseDouble(split[0]) * 30.48 + Double.parseDouble(split[1]) * 2.54, height);
                                } else {
                                    sendResultToHeightMachine(mb, Double.parseDouble(heightString) * 30.48, height);
                                }
                            }

                            RecordHeightDataRequest r = new RecordHeightDataRequest(httpChildId, new Date().getTime(), 1, "" + height, "CM");
                            new HttpHelper().recordHeight(r).subscribe(o -> {
//                                if (SBApplication.Companion.getStartCalculate().getValue()) {
//                                    SBApplication.Companion.getStartCalculate().setValue(false);
//                                }
                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
                                    cm.setHeight(height);
//                                    }
                                }

                                Message obtain = Message.obtain();
                                obtain.what = 555;
                                obtain.arg2 = 2;
                                obtain.arg1 = r.getChildId();
                                handler.sendMessage(obtain);
                            }, err -> {
                                Toast.makeText(SBApplication.applicationContext, err.getMessage(), Toast.LENGTH_SHORT).show();
                            });
                        } else {
                            if (SBApplication.Companion.getAppForeground().getValue()) {
                                sendReturnHeightBean = mb;
                                Intent i = new Intent(SBApplication.applicationContext, GetBlueToothDataActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.putExtra(KeyString.data, height);
                                i.putExtra(KeyString.type, 2);
                                i.putExtra(KeyString.cla, 1);
                                SBApplication.applicationContext.startActivity(i);
                            }
                        }
                    } else if (dataSplit[8].equals("1")) {
                        BigInteger bigCm = new BigInteger(dataSplit[4], 16);
                        BigInteger smallCm = new BigInteger(dataSplit[5], 16);
                        double height;
                        if (smallCm.toString().length() == 1) {
                            height = Double.parseDouble((bigCm.toString()) + ".0" + smallCm.toString());
                        } else {
                            height = Double.parseDouble((bigCm.toString()) + "." + smallCm.toString());
                        }
                        LogUtils.Companion.d("hhm", "身高：" + height);

                        if (/*(SBApplication.Companion.getStartCalculate().getValue() && SBApplication.Companion.getCalculateType() == 2) ||*/ SBApplication.userData.getChildren().size() == 1) {
                            String heightString = "";
                            int httpChildId = -1;
//                            if (SBApplication.userData.getChildren().size() == 1) {
                            heightString = String.valueOf(SBApplication.userData.getChildren().get(0).getHeight());
//                                SBApplication.Companion.setCalculateChild(SBApplication.userData.getChildren().get(0).getChildId());
                            httpChildId = SBApplication.userData.getChildren().get(0).getChildId();
//                                SBApplication.Companion.setCalculateType(2);
//                            } else {
//                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
//                                        heightString = String.valueOf(cm.getHeight());
//                                    }
//                                }
//                                httpChildId = SBApplication.Companion.getCalculateChild();
//                            }

                            double heightNum = 0.0;
                            if (!TextUtils.isEmpty(heightString)) {
                                heightNum = Double.parseDouble(heightString);
                            }
                            if (SBApplication.Companion.getUserData().getHeightUnit().equals("CM")) {
                                sendResultToHeightMachine(mb, heightNum, height);
                            } else {
                                if (heightString.contains(".")) {
                                    String[] split = heightString.split("\\.");
                                    sendResultToHeightMachine(mb, Double.parseDouble(split[0]) * 30.48 + Double.parseDouble(split[1]) * 2.54, height);
                                } else {
                                    sendResultToHeightMachine(mb, Double.parseDouble(heightString) * 30.48, height);
                                }
                            }
                            sendResultToHeightMachine(mb, heightNum, height);

                            RecordHeightDataRequest r = new RecordHeightDataRequest(httpChildId, new Date().getTime(), 1, "" + height, "CM");
                            new HttpHelper().recordHeight(r).subscribe(o -> {
//                                if (SBApplication.Companion.getStartCalculate().getValue()) {
//                                    SBApplication.Companion.getStartCalculate().setValue(false);
//                                }
                                for (ChildModel cm : SBApplication.userData.getChildren()) {
//                                    if (cm.getChildId() == SBApplication.Companion.getCalculateChild()) {
                                    int tmpFoot = (int) (height * 100 / 3048);
                                    int tmpInchDecade = (int) (((((height * 100 % 3048) / 254.0) * 10) + 5) / 100);
                                    int tmpInchUnit = (int) (((((height * 100 % 3048) / 254.0) * 10) + 5) % 100 / 10);
                                    String heightS;
                                    if (tmpInchDecade == 1 && tmpInchUnit == 2) {
                                        heightS = "" + (tmpFoot + 1);
                                    } else {
                                        if (tmpInchDecade == 0) {
                                            if (tmpInchUnit == 0) {
                                                heightS = "" + tmpFoot;
                                            } else {
                                                heightS = tmpFoot + "." + tmpInchUnit;
                                            }
                                        } else {
                                            heightS = tmpFoot + "." + tmpInchDecade + "" + tmpInchUnit;
                                        }
                                    }
                                    cm.setHeight(Double.parseDouble(heightS));
//                                    }
                                }

                                Message obtain = Message.obtain();
                                obtain.what = 555;
                                obtain.arg2 = 2;
                                obtain.arg1 = r.getChildId();
                                handler.sendMessage(obtain);
                            }, err -> {
                                Toast.makeText(SBApplication.applicationContext, err.getMessage(), Toast.LENGTH_SHORT).show();
                            });
                        } else {
                            if (SBApplication.Companion.getAppForeground().getValue()) {
                                sendReturnHeightBean = mb;
                                Intent i = new Intent(SBApplication.applicationContext, GetBlueToothDataActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                i.putExtra(KeyString.data, height);
                                i.putExtra(KeyString.type, 2);
                                i.putExtra(KeyString.cla, 2);
                                SBApplication.applicationContext.startActivity(i);
                            }
                        }
                    }
                }
            } else if (stringData.startsWith("a7 0 5 2 58")) {//身高仪回复语音开关指令
                if (stringData.startsWith("a7 0 5 2 58 0")) {//成功
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.VoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceOpenSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.VoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceCloseSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                } else if (stringData.startsWith("a7 0 5 2 58 1")) {//失败
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.VoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceCloseSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.VoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.voiceOpenSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                }
            } else if (stringData.startsWith("a7 0 5 2 98")) {//身高仪回复简化语音开关指令
                if (stringData.startsWith("a7 0 5 2 98 0")) {//成功
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.SimpleVoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceOpenSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.SimpleVoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceCloseSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                } else if (stringData.startsWith("a7 0 5 2 98 1")) {//失败
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.SimpleVoiceOpen) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceCloseSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.SimpleVoiceClose) {
                        if (controlCallBack != null) {
                            controlCallBack.simpleVoiceOpenSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                }
            } else if (stringData.startsWith("a7 0 5 2 a8")) {//身高仪回复单位控制指令
                if (stringData.startsWith("a7 0 5 2 a8 0")) {//成功
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.classCMtoFT) {
                        if (controlCallBack != null) {
                            controlCallBack.classCMtoFTSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.classFTtoCM) {
                        if (controlCallBack != null) {
                            controlCallBack.classFTtoCMSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                } else if (stringData.startsWith("a7 0 5 2 a8 1")) {//失败
                    int buttonStateChange = mb.getButtonStateChange();
                    if (buttonStateChange == MyBlueToothBean.classCMtoFT) {
                        if (controlCallBack != null) {
                            controlCallBack.classFTtoCMSuccess(mb.getBluetoothCode());
                        }
                    } else if (buttonStateChange == MyBlueToothBean.classFTtoCM) {
                        if (controlCallBack != null) {
                            controlCallBack.classCMtoFTSuccess(mb.getBluetoothCode());
                        }
                    }
                    mb.setButtonStateChange(MyBlueToothBean.ButtonDefault);
                }
            } else if (stringData.startsWith("a7 0 5 4 88")) {//身高仪设备可调试状态返回
                String[] s = stringData.split(" ");
                if (s[5].equals("0")) {//语音   0 开  1  关
                    mb.voiceTrigger = true;
                    if (controlCallBack != null) {
                        controlCallBack.voiceOpenSuccess(mb.getBluetoothCode());
                    }
                } else if (s[5].equals("1")) {
                    mb.voiceTrigger = false;
                    if (controlCallBack != null) {
                        controlCallBack.voiceCloseSuccess(mb.getBluetoothCode());
                    }
                }
                if (s[6].equals("0")) {//简化语音   0 开  1  关
                    mb.simpleVoiceTrigger = true;
                    if (controlCallBack != null) {
                        controlCallBack.simpleVoiceOpenSuccess(mb.getBluetoothCode());
                    }
                } else if (s[6].equals("1")) {
                    mb.simpleVoiceTrigger = false;
                    if (controlCallBack != null) {
                        controlCallBack.simpleVoiceCloseSuccess(mb.getBluetoothCode());
                    }
                }
                if (s[7].equals("0")) {//当前单位   0 cm  1  ft
                    mb.classTrigger = 0;
                    if (controlCallBack != null) {
                        controlCallBack.classFTtoCMSuccess(mb.getBluetoothCode());
                    }
                } else if (s[7].equals("1")) {
                    mb.classTrigger = 1;
                    if (controlCallBack != null) {
                        controlCallBack.classCMtoFTSuccess(mb.getBluetoothCode());
                    }
                }
            }
        }
    }

    public interface ControlCallBack {
        void voiceOpenSuccess(String bluetoothCode);

        void voiceCloseSuccess(String bluetoothCode);

        void shoeSizeOpenSuccess(String bluetoothCode);//体重秤专用

        void shoeSizeCloseSuccess(String bluetoothCode);//体重秤专用

        void simpleVoiceOpenSuccess(String bluetoothCode);

        void simpleVoiceCloseSuccess(String bluetoothCode);

        void classCMtoFTSuccess(String bluetoothCode);//身高仪专用

        void classFTtoCMSuccess(String bluetoothCode);//身高仪专用

        void classLBtoKGSuccess(String bluetoothCode);//体重秤专用

        void classKGtoLBSuccess(String bluetoothCode);//体重秤专用
    }

    public interface CallBack {
        void hasConnect();

        void connectSuccess(MyBlueToothBean myBlueToothBean);

        void connectFail();

    }

    public void setCallback(CallBack callback) {
        callBack = callback;
    }

    public void setControlCallback(ControlCallBack controlCallBack) {
        this.controlCallBack = controlCallBack;
    }

    private String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public ArrayList<MyBlueToothBean> getConnectDevice() {
        return connectDevice;
    }

    public void finish() {
        Bus.getDefault().unregister(this);
        handler.removeCallbacksAndMessages(null);
    }
}
